var EnergyModel = function (energy) {
  BaseModel.call(this);

  var self = this;
  var fn = {
    initData: function (energy) {
      var fields = ['no', 'energyType', 'imgUrl', 'monthMaxCount', 'yearMaxCount', 'name', 'address', 'workShopName'];
      self.setDataFromObj(energy, fields);

      var type = this.getType();
      self.type = type;

      var monthMaxCount = self.getData('monthMaxCount', 'number');
      self.hasMul = monthMaxCount < 10;
      self.monthMaxCount = monthMaxCount;
      self.yearMaxCount = self.getData('yearMaxCount', 'number');
      if (type !== 'electric') {
        self.monthMaxCount *= 1000;
        self.yearMaxCount *= 1000;
      }

      var dayList = energy.dayList;
      var energyRecordList = [];
      for (var i = 0; i < dayList.length; i++) {
        var dayData = dayList[i];
        var dayTime = dayData.dayTime;
        if (SysUtil.isEmpty(dayTime)) {
          continue;
        }

        var useDegee = dayData.useDegee;
        useDegee = Number(useDegee);
        if (type !== 'electric') {
          if (self.hasMul) {
            useDegee *= 1000;
          } else {
            self.chartUnit = 'm³';
          }
        }
        energyRecordList.push({
          dayTime: dayTime,
          value: useDegee
        });
      }

      var dateList = _.map(energyRecordList, 'dayTime');
      var minDate = moment().startOf('month');
      while (moment().isSameOrAfter(minDate, 'day')) {
        var date = minDate.format('YYYY-MM-DD');
        var index = dateList.indexOf(date);
        if (index === -1) {
          energyRecordList.push({
            dayTime: date,
            value: 0
          });
        }
        minDate.add(1, 'day');
      }

      energyRecordList = _.orderBy(energyRecordList, [function (record) {
        return moment(record.dayTime).valueOf();
      }], ['asc']);

      self.energyRecordList = energyRecordList;
    },

    getType: function () {
      var energyType = self.energyType;
      var type = '';
      switch (energyType) {
        case 1:
          type = "electric";
          self.unit = 'kw·h';
          self.chartUnit = 'kw·h';
          self.chartName = '用电量';
          break;
        case 2:
          type = "water";
          self.unit = 'L';
          self.chartUnit = 'L';
          self.chartName = '用水量';
          break;
        case 3:
          type = "gas";
          self.unit = 'L';
          self.chartUnit = 'L';
          self.chartName = '用气量';
          break;
      }
      return type;
    }
  };

  this.refreshData = function (energy) {
    var todayUseCount = energy.todayUseCount;
    todayUseCount = Number(todayUseCount);

    var energyRecordList = this.energyRecordList;
    var todayRecord = energyRecordList[energyRecordList.length - 1];
    if (this.type !== 'electric') {
      this.todayUseCount = todayUseCount * 1000;
      if (this.hasMul) {
        todayRecord.value = todayUseCount * 1000;
      } else {
        todayRecord.value = todayUseCount;
      }
    } else {
      this.todayUseCount = todayUseCount;
      todayRecord.value = todayUseCount;
    }

    var indicationNum = energy.numberStr;
    this.indicationNum = Number(indicationNum);
  };

  fn.initData(energy);
}
